package com.yuexiang.melodyhub.gateway.exception;

import com.yuexiang.melodyhub.common.result.ResultData;
import com.yuexiang.melodyhub.common.result.ReturnCode;
import com.yuexiang.melodyhub.common.utils.VerificationUtil;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ValidException {

    public static Mono<Void> unauthorized(ServerWebExchange exchange, String message) {
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        exchange.getResponse().getHeaders().add("Content-Type", "application/json");
        return exchange.getResponse().writeWith(Flux.just(exchange.getResponse().bufferFactory().wrap(VerificationUtil.convertToJson(ResultData.fail(ReturnCode.UNAUTHORIZED.getCode(), message)).getBytes())));
    }

}
